package com.mxchip.livestar.ui.call

import android.os.Looper
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

object AVRepository {

    private val _extMic by lazy { MutableLiveData<Boolean>(false) }

    val extMic: LiveData<Boolean>
        get() = _extMic

    fun extMic(value: Boolean) {
        if (Looper.myLooper() == Looper.getMainLooper())
            _extMic.value = value
        else {
            _extMic.postValue(value)
        }
    }

    private val _resShareResponse: MutableLiveData<Boolean> = MutableLiveData(false)

    private val resShareResponse: LiveData<Boolean>
        get() = _resShareResponse

    fun resShareResponse(value: Boolean) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            _resShareResponse.value = value
        } else {
            _resShareResponse.postValue(value)
        }
    }

    val openExtMic: MediatorLiveData<Boolean> = MediatorLiveData<Boolean>().apply {
        value = false
    }

    private val openExtMicObserver = Observer<Any> {
        Log.d("TAG", "openExtMicObserver: hdmiResponse.value:${resShareResponse.value}   extMic.value:${extMic.value}")
        openExtMic.value = resShareResponse.value!! && extMic.value!!
    }


    init {

        openExtMic.addSource(resShareResponse, openExtMicObserver)
        openExtMic.addSource(extMic, openExtMicObserver)
    }



    fun clear() {
        _resShareResponse.value = false
    }

}